<?php

/**
 * This is the model class for table "exposition".
 *
 * The followings are the available columns in table 'exposition':
 * @property string $exposition_id
 * @property string $user_id
 * @property string $country
 * @property string $city
 * @property string $address
 * @property string $picture_src
 * @property string $from_date
 * @property string $to_date
 * @property string $describe
 *
 * The followings are the available model relations:
 * @property User $user
 */
class Exposition extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Exposition the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'exposition';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, from_date, to_date, name', 'required'),
			array('user_id', 'length', 'max'=>10),
			array('country, city', 'length', 'max'=>50),
			array('address, picture_src', 'length', 'max'=>255),
			array('describe, from_date, to_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('exposition_id, user_id, country, city, address, picture_src, from_date, to_date, describe', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(                        
			'exposition_id' => 'Выставка',
			'user_id' => 'Пользователь',
			'country' => 'Страна',
			'city' => 'Город',
			'address' => 'Адрес',
			'picture_src' => 'Изображение',
			'from_date' => 'Дата начала',
			'to_date' => 'Дата окончания',
                        'name'      => 'Название выставки',
			'describe' => 'Описание',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('exposition_id',$this->exposition_id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('country',$this->country,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('picture_src',$this->picture_src,true);
		$criteria->compare('from_date',$this->from_date,true);
		$criteria->compare('to_date',$this->to_date,true);
		$criteria->compare('describe',$this->describe,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
        
        public function beforeValidate()
        {
            if(parent::beforeValidate())
            {
                $this->from_date = date('Y-m-d H:i:s', $this->from_date);
                $this->to_date = date('Y-m-d H:i:s', $this->to_date);
                return true;
            }
            return false;
        }
}